<template>
    <span v-if="type == 'format'">
        {{ timestampToView(format) }}
    </span>
    <span v-else-if="type == 'year'">
        {{ timestampToView('YYYYZ') }}</span>
    <span v-else-if="type == 'month'">
        {{ timestampToView('YYYY-MMZ') }}</span>
    <span v-else-if="type == 'day'">
        {{ timestampToView('YYYY-MM-DDZ') }}</span>
    <span v-else-if="type == 'week'">
        {{ timestampToView($t('timestamp.week')) }}</span>
    <span v-else-if="type == 'week-day'">
        {{ timestampToView('YYYY-MM-DDZ') }} ~ {{ timestampToView('YYYY-MM-DDZ', timestampByJS + 86400 * 6000) }}</span>
    <span v-else-if="type == 'hour'">
        {{ timestampToView('YYYY-MM-DDTHH:00Z') }}</span>
    <span v-else-if="type == 'minute'">
        {{ timestampToView('YYYY-MM-DDTHH:mmZ') }}</span>
    <span v-else-if="type == 'second'">
        {{ timestampToView('YYYY-MM-DDTHH:mm:ssZ') }}</span>
    <span v-else>
        {{ timestampToView() }}
    </span>
</template>

<script>
export default {
    name: "Timestamp",
    props: {
        type: {
            type: String,
            default: 'default'
        },
        timestamp: {
            type: [String, Number],
            default: 0
        },
        format: String,
        timezone: String,
    },
    computed: {
        timestampByJS: function () {
            // 转换为数字
            let timestamp = Number(this.timestamp);
            // 转换为毫秒
            return timestamp > 10000000000 ? timestamp : timestamp * 1000;
        }
    },
    methods: {
        // 将时间戳转换为可视时间
        timestampToView: function (format, timestamp) {
            if (this.timezone) {
                return this.$helper.makeTimestampToDatetime(timestamp || this.timestampByJS, format, this.timezone);
            } else {
                return this.$store.getters['config/filterTimestampToDatetime'](timestamp || this.timestampByJS, format);
            }
        }
    },
}
</script>

<style scoped>

</style>
